package dao;

import java.io.*;
import java.util.*;
import model.Contact;
import exception.ContactException;
import model.FriendContact;
import model.WorkContact;

public class FileContactDAO implements ContactDAO {
    private static final String FILE_PATH = "contacts.txt";
    private List<Contact> contacts;

    public FileContactDAO() {
        contacts = new ArrayList<>();
        loadContacts();
    }

    private void loadContacts() {
        contacts.clear();
        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length >= 5) {
                    Contact contact;
                    if (parts[0].equals("朋友")) {
                        contact = new FriendContact(parts[1], parts[2], parts[3], parts[4]);
                    } else {
                        contact = new WorkContact(parts[1], parts[2], parts[3], parts[4]);
                    }
                    contacts.add(contact);
                }
            }
        } catch (IOException e) {
            // 如果文件不存在，使用空列表
        }
    }

    private void saveContacts() {
        try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_PATH))) {
            for (Contact contact : contacts) {
                String extraInfo = contact instanceof FriendContact ?
                    ((FriendContact)contact).getBirthday() :
                    ((WorkContact)contact).getCompany();

                writer.println(String.format("%s,%s,%s,%s,%s",
                    contact.getType(),
                    contact.getName(),
                    contact.getPhone(),
                    contact.getEmail(),
                    extraInfo));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addContact(Contact contact) throws ContactException {
        if (findContact(contact.getName()) != null) {
            throw new ContactException("联系人已存在！");
        }
        contacts.add(contact);
        saveContacts();
    }

    @Override
    public void deleteContact(String name) throws ContactException {
        Contact contact = findContact(name);
        if (contact == null) {
            throw new ContactException("联系人不存在！");
        }
        contacts.remove(contact);
        saveContacts();
    }

    @Override
    public void updateContact(Contact contact) throws ContactException {
        deleteContact(contact.getName());
        addContact(contact);
    }

    @Override
    public List<Contact> getAllContacts() {
        return new ArrayList<>(contacts);
    }

    @Override
    public Contact findContact(String name) {
        return contacts.stream()
                .filter(c -> c.getName().equals(name))
                .findFirst()
                .orElse(null);
    }
}
